﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using FlickrNet;
using FlyingAPI;

delegate void LogAppend(String Message);

namespace TagSpider
{
    public partial class Form1 : Form
    {
        private String url;

        public String Url
        {
            get { return url; }
            set { url = value; }
        }
        private TagSpiderController controller;
        private List<SearchJob> searchJobs;

        public List<SearchJob> SearchJobs
        {
            get { return searchJobs; }
            set
            {
                searchJobs = value;
                if (searchJobs == null)
                {
                    return;
                }

                foreach (SearchJob searchJob in searchJobs)
                {
                    cbConcept.Items.Add(searchJob.Concept);
                }
            }
        }


        public Form1()
        {
            InitializeComponent();

            controller = TagSpiderController.getInstance(this.WritelnLog);
            RTB_Log.HideSelection = false;
        }

        private void BTN_Start_Click(object sender, EventArgs e)
        {
            String validate = validateInput();

            if (String.IsNullOrEmpty(validate))
            {
                foreach(SearchJob searchJob in searchJobs)
                {
                    if(searchJob.Concept.Equals(cbConcept.Text))
                    {
                        controller.SearchJob = searchJob;
                    }
                }
                controller.start();
            }
            else
            {
                // 提示出错
                MessageBox.Show(validate, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void WritelnLog(String s)
        {
            if (RTB_Log.InvokeRequired)
            {
                LogAppend d = new LogAppend(WritelnLog);
                this.Invoke(d, new object[] { s });
            }
            else
            {
                RTB_Log.AppendText(DateTime.Now.ToString() + ": " + s + "\n");
                //RTB_Log.Text += DateTime.Now.ToString() + ": " + s + "\n";
                //// RTB_Log.Focus();
                //RTB_Log.Select(RTB_Log.Text.Length, 0);
                //RTB_Log.ScrollToCaret();
            }
        }

        private String validateInput()
        {
            if (String.IsNullOrEmpty(cbConcept.Text))
            {
                return "Missing Concept!";
            }

            return "";
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // controller.Factory.stopImmediately();
        }

        private void bt_pause_Click(object sender, EventArgs e)
        {
            WritelnLog("=============START TO PAUSE, WAIT FOR CUR THREADS FINISHED==============");
            controller.pause();
        }
    }
}
